package com.culture.config.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

@UtilityClass
@Slf4j
public class ResourceUtil {
	
	public static boolean writeClassResourceFile(HttpServletResponse reponse, String resourceFileName) {
		InputStream ins = null;
		boolean ret = false;
		try {			
			reponse.setContentType(mimeType(StringUtils.substringAfter(resourceFileName, ".")));			
			ClassPathResource classPathResource = new ClassPathResource(resourceFileName);
			if(!classPathResource.exists()) {
				log.warn("no resource for:{}",resourceFileName);
				return ret;
			}
			ins = classPathResource.getInputStream();			
			byte[] fileData = ins.readAllBytes();		
			reponse.setContentLengthLong(fileData.length);
			reponse.getOutputStream().write(fileData);
			reponse.flushBuffer();
			ret = true;
		} catch (IOException e) {
			log.error("writeClassResourceFile IOException:{}",e);
		} catch(Exception ex) {
			log.error("writeClassResourceFile exception:{}",ex);
		} finally {
			try {
				if(ins != null) {
					ins.close();
				}
			} catch(Exception ex) {
				log.error("writeClassResourceFile close resource:{}",ex);
			}
		}
		return ret;		
	}	

	public static boolean writeClassResourceFile(HttpServletResponse reponse,String resourceFileName, Object obj) {
		boolean ret = false;		
		try{
			String outData = FreemarkerUtil.buildTemplate(resourceFileName,obj);
			if(StringUtils.isBlank(outData)) {
				log.warn("failed build TemplateContent from resource :{}",resourceFileName);
				return ret;
			}
			byte[] fileData = StringUtils.getBytes(outData,StandardCharsets.UTF_8);		
			reponse.setContentType(mimeType(StringUtils.substringAfter(resourceFileName, ".")));	
			reponse.setContentLength(fileData.length);
			reponse.getOutputStream().write(fileData);
			reponse.flushBuffer();			
			ret = true;
		} catch(Exception ex) {
			log.error("writeClassResourceFile exception:{}",ex);
		}
		return ret;
	}
	
	public static String mimeType(String type) {
		Map<String,String> mimeMap = new HashMap<>();
		if(MapUtils.isEmpty(mimeMap)) {
			mimeMap.put("htm", "text/html");
			mimeMap.put("html", "text/html");
			mimeMap.put("js", "application/x-javascript");
			mimeMap.put("js.map", "application/x-javascript");
			mimeMap.put("css", "text/css");
			mimeMap.put("css.map", "text/css");
			mimeMap.put("jpeg", "image/jpeg");			
			mimeMap.put("jpg", "image/jpeg");
			mimeMap.put("png", "image/png");
			mimeMap.put("icon", "image/x-icon");
			mimeMap.put("json", "application/json");
			mimeMap.put("pdf", "application/pdf");
			mimeMap.put("txt", "text/plain");
		}
		return mimeMap.getOrDefault(type, "application/octet-stream");
	}


}
